package com.huangbc.servicemap.remote;

import com.alibaba.fastjson.JSONObject;
import com.huangbc.internalcommon.constant.AmapConfigConstants;
import com.huangbc.internalcommon.dto.ResponseResult;
import com.huangbc.internalcommon.dto.ServiceRequest;
import com.huangbc.internalcommon.dto.ServiceListResult;
import com.huangbc.internalcommon.response.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Derek.huang on 2024/3/29 19:25.
 */
@Service
public class ServiceClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult add(String name){
        // &key=<用户的key>
        // 拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_ADD_URL);
        url.append("?");
        url.append("key="+amapKey);
        url.append("&");
        url.append("name="+name);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null,String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        ServiceResponse serviceResponse = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("data")), ServiceResponse.class);
        return ResponseResult.success(serviceResponse);
    }

    public ResponseResult list(String amapKey){
        // &key=<用户的key>
        // 拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_LIST_URL);
        url.append("?");
        url.append("key="+amapKey);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(url.toString(),String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        List<ServiceResponse> serviceResponseList = new ArrayList<>();

        if(jsonObject.get("errmsg").equals("OK")){
            ServiceListResult serviceResult = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("data")), ServiceListResult.class);
            serviceResponseList = serviceResult.getResults();
        }
        return ResponseResult.success(serviceResponseList);
    }

    public ResponseResult delete(ServiceRequest serviceRequest) {

        // &key=<用户的key>
        // 拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_DELETE_URL);
        url.append("?");
        url.append("key="+serviceRequest.getKey());
        url.append("&");
        url.append("sid="+serviceRequest.getSid());

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(),null,String.class);
        String entityBody = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(entityBody);
        String errmsg = jsonObject.getString("errmsg");
        if (errmsg.equals("OK") || errmsg.equals("SERVICE_NOT_FOUND")){
            return ResponseResult.success(errmsg);
        }
        return ResponseResult.fail("删除服务失败！");
    }
}

